/**
    Copyright (C) 2011  Alexander Vollmer

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.jefb.web.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jefb.sec.entity.UserRight;
import org.jefb.service.IUserSessionService;
import org.jefb.web.model.item.MenuItem;
import org.zkoss.zul.Image;
import org.zkoss.zul.Label;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.ListitemRenderer;
import org.zkoss.zul.Row;
import org.zkoss.zul.RowRenderer;


@SuppressWarnings("serial")
public class MenuModel extends ListModelList implements RowRenderer, ListitemRenderer{

	private static final long serialVersionUID = 1L;
	
	public enum MenuBlock{
		WORKSPACE, ADMINISTRATION, COMMON
	};
	
	//user workspace menu items
//	private final MenuItem workspaceItem = new MenuItem("workspace", "include/workspace/workspace.zul", "../img/workspace.png", UserRight.VIEW_WORKSPACE);
//	private final MenuItem inboxItem = new MenuItem("inbox", "include/workspace/inbox.zul","../img/inbox.png", UserRight.VIEW_INBOX);
//	private final MenuItem outboxItem = new MenuItem("outbox", "include/workspace/outbox.zul","../img/outbox.png", UserRight.VIEW_OUTBOX);
//	private final MenuItem inboxItem = new MenuItem("inbox", "<inbox />","../img/inbox.gif", UserRight.VIEW_INBOX);
//	private final MenuItem outboxItem = new MenuItem("outbox", "<outbox />","../img/outbox.gif", UserRight.VIEW_OUTBOX);

//	private final MenuItem archiveItem = new MenuItem("archive", "include/workspace/archive.zul","../img/archive.png", UserRight.VIEW_ARCHIVE);
//	private final MenuItem syncItem = new MenuItem("sync", "include/workspace/archive.zul","../img/sync.png", UserRight.VIEW_ARCHIVE);
	
	//common workspace menu items
//	private final MenuItem commonWorkspaceItem = new MenuItem("common workspace", "include/cworkspace/cworkspace.zul", "../img/workspace.png", UserRight.VIEW_COMMON_WORKSPACE);
//	private final MenuItem commonInboxItem = new MenuItem("inbox", "include/cworkspace/cinbox.zul","../img/inbox.png", UserRight.VIEW_COMMON_INBOX);
//	private final MenuItem commonOutboxItem = new MenuItem("common outbox", "include/cworkspace/coutbox.zul","../img/outbox.png", UserRight.VIEW_COMMON_OUTBOX);
//	private final MenuItem commonArchivItem = new MenuItem("common archive", "include/cworkspace/carchive.zul","../img/archive.png", UserRight.VIEW_COMMON_ARCHIVE);
//	private final MenuItem commonPushItem = new MenuItem("push", "include/cworkspace/cpush.zul","../img/fs16/push.png", UserRight.VIEW_COMMON_PUSH);
	private final MenuItem commonBaseItem = new MenuItem("workspace", "include/cworkspace/cbase.zul","../img/fs16/basedir.png", UserRight.VIEW_COMMON_BASE);
	
//	private final MenuItem destinationsItem = new MenuItem("destinations", "include/admin/destinations.zul","../img/destinations.png", UserRight.VIEW_DESTINATIONS);
	private final MenuItem rolesItem = new MenuItem("roles", "include/admin/roles.zul","../img/fs16/roles.png", UserRight.VIEW_ROLES);
	private final MenuItem usersItem = new MenuItem("users", "include/admin/users.zul","../img/fs16/users.png", UserRight.VIEW_USERS);
//	private final MenuItem appLogItem = new MenuItem("application log", "include/admin/app_log.zul","../img/log.png", UserRight.VIEW_APP_LOG);
//	private final MenuItem actionLogItem = new MenuItem("action log", "include/admin/action_log.zul","../img/log.png", UserRight.VIEW_ACTION_LOG);
//	private final MenuItem settingsItem = new MenuItem("settings", "include/admin/settings.zul","../img/settings.png", UserRight.VIEW_SETTINGS);
//	private final MenuItem appMonitorItem = new MenuItem("application monitor", "include/admin/app_monitor.zul","../img/monitor.png", UserRight.VIEW_APP_MONITOR);
	
	
	//administration menu items
	
//	private List<MenuItem> userWorkspaceMenuItems = new ArrayList<MenuItem>(){
//		{
//			add(workspaceItem);
//			add(inboxItem);
//			add(outboxItem);
//			add(archiveItem);
//			add(syncItem);
//		}
//	};
	
	private List<MenuItem> commonWorkspaceMenuItems = new ArrayList<MenuItem>(){
		{
			add(commonBaseItem);
//			add(commonWorkspaceItem);
//			add(commonInboxItem);
//			add(commonOutboxItem);
//			add(commonArchivItem);
//			add(commonPushItem);
		}
	};
	
	private List<MenuItem> adminMenuItems = new ArrayList<MenuItem>(){
		{
//			add(destinationsItem);
			add(rolesItem);
			add(usersItem);
//			add(appLogItem);
//			add(actionLogItem);
//			add(settingsItem);
//			add(appMonitorItem);
		}
	};
	
	private Map<MenuBlock, List<MenuItem>> menuMap = new HashMap<MenuBlock, List<MenuItem>>(){
		{
			//put(MenuBlock.WORKSPACE, userWorkspaceMenuItems);
			put(MenuBlock.COMMON, commonWorkspaceMenuItems);
			put(MenuBlock.ADMINISTRATION, adminMenuItems);
		}
	};
	
	public MenuModel(){
		super();
	}
	
	@SuppressWarnings("unchecked")
	public MenuModel(MenuBlock menuBlock, IUserSessionService userSessionService){
		super();
		for(MenuItem menuItem: menuMap.get(menuBlock)){
			if(userSessionService.hasRight(menuItem.getUserPermission())){
				this._list.add(menuItem);
			}
		}
	}

	public void render(Listitem listItem, Object data) throws Exception {
		MenuItem menuItem = (MenuItem)data;
		if(menuItem.getImage()!=null){
			Listcell iconCell = new Listcell();
			iconCell.setImage(menuItem.getImage());
			iconCell.setParent(listItem);
		}
		
		Listcell titleCell = new Listcell(menuItem.getTitle());		
		titleCell.setParent(listItem);
		listItem.setAttribute("menuItem", menuItem);
	}

	public void render(Row row, Object data) throws Exception {
		MenuItem menuItem = (MenuItem)data;
		if(menuItem.getImage()!=null){
			Image image = new Image();
			image.setSrc(menuItem.getImage());
			image.setParent(row);
		}
		Label titleLabel = new Label(menuItem.getTitle());
		titleLabel.setParent(row);
	}

}
